import config from '@/config/config.js';
import http from "@/api/http.js";

const DetectionRequest ={
    loadDetection(detection,page=1,limit=10,callBack){
        let token = localStorage.getItem("token");
        if(token){
            let url = config.defectBaseUrl + "/detection/list?page="+page + "&limit="+limit;
            if(detection){
                if (detection.detectionId){
                    url += "&detectionId=" + detection.detectionId;

                }
                if(detection.detectionName){
                    url += "&detectionName=" + detection.detectionName;
                }
                if (detection.detectionType){
                    url += "&defectType=" + detection.detectionType;
                }
            }
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },
    saveDetection(detection,callBack){
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.defectBaseUrl + "/detection/save";
            http.post(url,detection,callBack);
        }else {
            callBack(undefined);
        }
    },
    deleteDetection(detectionId,callback){
        let token = localStorage.getItem("token");
        if(token){
            let url = config.defectBaseUrl + "/detection/delete?detectionId="+detectionId;
            http.delete(url,callback)
        }else {
            callback(undefined);
        }
    },
    selectById(detectionId,callBack){
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.defectBaseUrl + "/detection/get?detectionId=" + detectionId;
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },
    updateDefectById(detection,callBack) {
        let token = localStorage.getItem("token");
        if (token) {
            let url = config.defectBaseUrl + "/detection/update";
            http.put(url,detection,callBack);
        } else {
            callBack(undefined);
        }
    },
    export(detection,page=1,limit=10,callBack){
        let token = localStorage.getItem("token");
        if (token){
            let url = config.defectBaseUrl + "/detection/export?page=" + page + "&limit=" + limit;
            if(detection){
                if(detection.detectionName)
                    url += '&detectionName=' + detection.detectionName;
                if(detection.detectionType)
                    url += '&detectionType=' + detection.detectionType;
                if(detection.detectionTool)
                    url += '&detectionTool=' + detection.detectionTool;
            }
            http.get(url,callBack);
        }else {
            callBack(undefined);
        }
    },




}

export default DetectionRequest
